package com.smyh.protcol.device;

import com.smyh.UIM.record.DeviceSingleRecordFm;
import com.smyh.UIM.record.HostFaultRecordFm;
import com.smyh.UIM.record.HostSyncRecordFm;
import com.smyh.bean.CmdDataModel;
import com.smyh.cache.UIM.HostFaultRecordUI;
import com.smyh.cache.UIM.HostSyncRecordUI;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.cache.UIM.right.file_update.UpdateProgramUICache;
import com.smyh.commcache.commd.CommandCache;
import com.smyh.model.host.record.HostFaultRecordModel;
import com.smyh.model.host.record.HostSyncRecordModel;
import com.smyh.protcol.host.Protocol101Struts;
import com.smyh.server.SuperAddRecordPointTread;
import com.smyh.server.ToastShowMsgThread;
import com.smyh.util.AddCrc;
import com.smyh.util.DateUtil101V2;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class DeviceProtcolAnaly {

    public static void analyMsg(IoSession session, IoBuffer buffer) {
        Object obj = session.getAttribute("DEVICE_LAST_BUF");
        IoBuffer totalBuf = IoBuffer.allocate(0).setAutoExpand(true);
		/*
		 * 把上次session数据和并到本次数据
		 */
        if (obj != null) {
            totalBuf.put((IoBuffer) obj);
            session.removeAttribute("DEVICE_LAST_BUF");
        }
        totalBuf.put(buffer).flip();
        /**
         * 断开连接的开关：数据-固定格式-固定数据:
         */
        String allStr = totalBuf.getHexDump();
        if (allStr.contains("55 AA 08 D2 09 00 E3 16")) {
            MainCahce.on_offLineAllRF();
        }

        for (int i = 0; i < totalBuf.limit(); i++) {
            if (totalBuf.limit() < i + 6) {
                IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
                leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit() - i)).flip();
                session.setAttribute("DEVICE_LAST_BUF", leaveBuf);
                System.out.println("存入通道数据!");
                return;
            }
            int head0 = totalBuf.get(i) & 0xff;
            int head1 = totalBuf.get(i + 1) & 0xff;
            int head2 = totalBuf.get(i + 2) & 0xff;
            int head3 = totalBuf.get(i + 3) & 0xff;

            if (0xa4 == head0 && 0xa8 == head1) {
                int length = head2;
                if (length + i <= totalBuf.limit()) {
                    // *********进入一个自定义报文过程*************
                    byte crcSum = totalBuf.get(i + length - 2);
                    byte endFix = totalBuf.get(i + length - 1);
                    if (endFix != Protocol101Struts.FIX_FRAME_END) {
                        // 格式不对，判断继续
                        continue;
                    }

                    // 校验码是除去 ----报文头----尾----的其他字节和
                    byte countCrc = AddCrc.getCrc(totalBuf, i + 2, i + length - 3);
                    if (crcSum == countCrc) {

                        // 得到设备回应
                        session.setAttribute("READ_FIRST", 0);
                        // 报文类型判定：
                        switch (head3) {
                            case 0xd9: {
                                if(length < (7+164)){
                                    // 长度不足-指针跳到数据尾部
                                    i += length;
                                    continue;
                                }
                                //指示器单波数据：82字节+82字节：电流-电压数据
                                WaveDataCacheTemp.totalPack = (byte) (totalBuf.get(i + 4) & 0xff);
//							    WaveDataCacheTemp.currentType = (byte) 0xDC;
//							    UIElement.chooseDeviceAddr;
                                List<Integer> d9ElecList = new ArrayList<Integer>();
                                List<Integer> d9BVoltList = new ArrayList<Integer>();

                                for(int dj = 0; dj<41; dj++ ){
                                    d9ElecList.add(totalBuf.getUnsignedShort(i + 5 + dj*2));
                                    d9BVoltList.add(totalBuf.getUnsignedShort(i + 5 + 82 + dj*2));
                                }


                                DeviceSingleRecordFm.singleRecordElecMap.put(MainCahce.chooseDeviceAddr, d9ElecList);
                                DeviceSingleRecordFm.singleRecordVoltMap.put(MainCahce.chooseDeviceAddr, d9ElecList);
                                DeviceSingleRecordFm.freshModel();
                                break;
                            }
                            case 0xdc: {
                                //指示器同步电流-数据
                                WaveDataCacheTemp.totalPack = (byte) (totalBuf.get(i + 4) & 0xff);
                                WaveDataCacheTemp.currentType = (byte) 0xDC;

                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 6, length - 8);
                                List<Integer> waveDatalist = new Vector<Integer>();
                                for (int jj = 0; jj < contentBuff.limit() / 2; jj++) {
                                    Integer dataTemp = contentBuff.getUnsignedShort(jj * 2);
                                    waveDatalist.add(dataTemp);
                                }
                                WaveDataCacheTemp.commCurrentMap.put(currentPack, waveDatalist);
                                CommandCache.commandMap.remove(CommandCache.SUPERADD_RECORD_POINT);
                                SuperAddRecordPointTread.lastRunTime = System.currentTimeMillis();
                                SuperAddRecordPointTread.isCheckData = true;
                                break;
                            }
                            case 0xdd: {
                                //指示器同步电压-数据
                                WaveDataCacheTemp.currentType = (byte) 0xDD;
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                WaveDataCacheTemp.totalPack = (byte) (totalBuf.get(i + 4) & 0xff);
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                WaveDataCacheTemp.commDDLastIndex = currentPack;

                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 6, length - 8);
                                List<Integer> waveDatalist = new Vector<Integer>();
                                for (int jj = 0; jj < contentBuff.limit() / 2; jj++) {
                                    Integer dataTemp = contentBuff.getUnsignedShort(jj * 2);
                                    waveDatalist.add(dataTemp);
                                }
                                WaveDataCacheTemp.commCurrentMap.put(currentPack, waveDatalist);
                                CommandCache.commandMap.remove(CommandCache.SUPERADD_RECORD_POINT);
                                SuperAddRecordPointTread.lastRunTime = System.currentTimeMillis();
                                SuperAddRecordPointTread.isCheckData = true;
                                break;
                            }
                            case 0xde: {
                                //指示器：故障电流
                                WaveDataCacheTemp.currentType = (byte) 0xDE;
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                WaveDataCacheTemp.totalPack = (byte) (totalBuf.get(i + 4) & 0xff);
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 6, length - 8);
                                if (currentPack == 0) {
                                    // 时间计算单位ms

                                    long syncTimes0 = ((long) contentBuff.get(0) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(1) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(2) & 0xff) * 256 + ((long) contentBuff.get(3) & 0xff);

                                    long syncTimes1 = ((long) contentBuff.get(4) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(5) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(6) & 0xff) * 256 + ((long) contentBuff.get(7) & 0xff);
                                    // 29134 28928 206 32768
                                    // long syncTimes = syncTimes0*500*1000 +
                                    // (syncTimes1*2*1000*1000)/65536;
                                    long syncTimesN = syncTimes0 * 16384 + syncTimes1;

                                    long faultTimes0 = ((long) contentBuff.get(8) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(9) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(10) & 0xff) * 256
                                            + ((long) contentBuff.get(11) & 0xff);

                                    long faultTimes1 = ((long) contentBuff.get(12) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(13) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(14) & 0xff) * 256
                                            + ((long) contentBuff.get(15) & 0xff);
                                    long faultTimes = faultTimes0 * 16384 + faultTimes1;

                                    long syncTimes2 = ((long) contentBuff.get(16) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(17) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(18) & 0xff) * 256
                                            + ((long) contentBuff.get(19) & 0xff);

                                    long syncTimes3 = ((long) contentBuff.get(20) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(21) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(22) & 0xff) * 256
                                            + ((long) contentBuff.get(23) & 0xff);
                                    long syncTimesN1 = syncTimes2 * 16384 + syncTimes3;

                                    long faultTimes2 = ((long) contentBuff.get(24) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(25) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(26) & 0xff) * 256
                                            + ((long) contentBuff.get(27) & 0xff);

                                    long faultTimes3 = ((long) contentBuff.get(28) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(29) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(30) & 0xff) * 256
                                            + ((long) contentBuff.get(31) & 0xff);
                                    long faultTimesSync = faultTimes2 * 16384 + faultTimes3;

                                    // long faultTimes = faultTimes0*500*1000 +
                                    // (faultTimes1*2*1000*1000)/65536;

                                    long varTimes = faultTimesSync - syncTimesN;// △t
                                    // △Ta=(
                                    // A相故障后的同步时刻-A相定时同步时刻N)
                                    long varTimesF = faultTimesSync - faultTimes; // △Fa=(A相故障后的同步时刻-A相故障时刻)
//
//                                    List<Long> varTimesList = DeviceRecordFaultAndSynCache.recordWaveDEVarTimesMap
//                                            .get(UIElement.chooseDeviceAddr);
//                                    List<Long> varTimesListf = DeviceRecordFaultAndSynCache.recordWaveDEVarTimesMapF
//                                            .get(UIElement.chooseDeviceAddr);
//                                    List<Integer> absTimesList = DeviceRecordFaultAndSynCache.recordWaveDEABSTimesMap
//                                            .get(UIElement.chooseDeviceAddr);
//                                    if (varTimesList == null) {
//                                        varTimesList = new Vector<Long>();
//                                        DeviceRecordFaultAndSynCache.recordWaveDEVarTimesMap.put(
//                                                UIElement.chooseDeviceAddr, varTimesList);
//                                    }
//                                    if (varTimesListf == null) {
//                                        varTimesListf = new Vector<Long>();
//                                        DeviceRecordFaultAndSynCache.recordWaveDEVarTimesMapF.put(
//                                                UIElement.chooseDeviceAddr, varTimesListf);
//                                    }
//                                    if (absTimesList == null) {
//                                        absTimesList = new Vector<Integer>();
//                                        DeviceRecordFaultAndSynCache.recordWaveDEABSTimesMap.put(
//                                                UIElement.chooseDeviceAddr, absTimesList);
//                                    }
//                                    if (varTimesList.size() >= 5) {
//                                        varTimesList.remove(4);
//                                    }
//                                    if (varTimesListf.size() >= 5) {
//                                        varTimesListf.remove(4);
//                                    }
//                                    varTimesList.add(0, varTimes);
//                                    varTimesListf.add(0, varTimesF);
//                                    if (absTimesList.size() >= 5) {
//                                        absTimesList.remove(4);
//                                    }
//                                    absTimesList.add(0, DeviceOptionFromCache.getSyncABSTimeToList().size() - 1);
//
                                    WaveDataCacheTemp.commCurrentMap.put(currentPack, new ArrayList<Integer>());
                                } else {
                                    List<Integer> waveDatalist = new Vector<>();
                                    for (int jj = 0; jj < contentBuff.limit() / 2; jj++) {
                                        Integer dataTemp = contentBuff.getUnsignedShort(jj * 2);
                                        waveDatalist.add(dataTemp);
                                    }
                                    WaveDataCacheTemp.commCurrentMap.put(currentPack, waveDatalist);
                                }
                                CommandCache.commandMap.remove(CommandCache.SUPERADD_RECORD_POINT);
                                SuperAddRecordPointTread.lastRunTime = System.currentTimeMillis();
                                SuperAddRecordPointTread.isCheckData = true;
                                break;
                            }
                            case 0xdf: {
                                //指示器：故障电压
                                WaveDataCacheTemp.currentType = (byte) 0xDF;
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                WaveDataCacheTemp.totalPack = (byte) (totalBuf.get(i + 4) & 0xff);
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 6, length - 8);
                               if (currentPack == 0) {
                                   // 时间计算单位ms

                                   long syncTimes0 = ((long) contentBuff.get(0) & 0xff) * 256 * 256 * 256
                                           + ((long) contentBuff.get(1) & 0xff) * 256 * 256
                                           + ((long) contentBuff.get(2) & 0xff) * 256 + ((long) contentBuff.get(3) & 0xff);

                                  long syncTimes1 = ((long) contentBuff.get(4) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(5) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(6) & 0xff) * 256 + ((long) contentBuff.get(7) & 0xff);

                                    // long syncTimes = syncTimes0*500*1000 +
                                    // (syncTimes1*2*1000*1000)/65536;
                                    long syncTimesN = syncTimes0 * 16384 + syncTimes1;

                                    long faultTimes0 = ((long) contentBuff.get(8) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(9) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(10) & 0xff) * 256
                                            + ((long) contentBuff.get(11) & 0xff);

                                    long faultTimes1 = ((long) contentBuff.get(12) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(13) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(14) & 0xff) * 256
                                            + ((long) contentBuff.get(15) & 0xff);

                                    // long faultTimes = faultTimes0*500*1000 +
                                    // (faultTimes1*2*1000*1000)/65536;
                                    long faultTimes = faultTimes0 * 16384 + faultTimes1;

                                    long syncTimes2 = ((long) contentBuff.get(16) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(17) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(18) & 0xff) * 256
                                            + ((long) contentBuff.get(19) & 0xff);

                                    long syncTimes3 = ((long) contentBuff.get(20) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(21) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(22) & 0xff) * 256
                                            + ((long) contentBuff.get(23) & 0xff);
                                    long syncTimesN1 = syncTimes2 * 16384 + syncTimes3;

                                    long faultTimes2 = ((long) contentBuff.get(24) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(25) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(26) & 0xff) * 256
                                            + ((long) contentBuff.get(27) & 0xff);

                                    long faultTimes3 = ((long) contentBuff.get(28) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(29) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(30) & 0xff) * 256
                                            + ((long) contentBuff.get(31) & 0xff);
                                    long faultTimesSync = faultTimes2 * 16384 + faultTimes3;

                                    // long faultTimes = faultTimes0*500*1000 +
                                    // (faultTimes1*2*1000*1000)/65536;

                                    long varTimes = faultTimesSync - syncTimesN;// △t
                                    // △Ta=(
                                    // A相故障后的同步时刻-A相定时同步时刻N)
                                    long varTimesF = faultTimesSync - faultTimes; // △Fa=(
                                    // A相故障后的同步时刻-A相故障时刻)
//                                    List<Long> varTimesList = DeviceRecordFaultAndSynCache.recordWaveDFVarTimesMap
//                                            .get(UIElement.chooseDeviceAddr);
//                                    List<Long> varTimesListf = DeviceRecordFaultAndSynCache.recordWaveDFVarTimesMapF
//                                            .get(UIElement.chooseDeviceAddr);
//                                    if (varTimesList == null) {
//                                        varTimesList = new Vector<Long>();
//                                        DeviceRecordFaultAndSynCache.recordWaveDFVarTimesMap.put(
//                                                UIElement.chooseDeviceAddr, varTimesList);
//                                    }
//                                    if (varTimesListf == null) {
//                                        varTimesListf = new Vector<Long>();
//                                        DeviceRecordFaultAndSynCache.recordWaveDFVarTimesMapF.put(
//                                                UIElement.chooseDeviceAddr, varTimesListf);
//                                    }
//                                    if (varTimesList.size() >= 5) {
//                                        varTimesList.remove(4);
//                                    }
//                                    if (varTimesListf.size() >= 5) {
//                                        varTimesListf.remove(4);
//                                    }
//                                    varTimesList.add(0, varTimes);
//                                    varTimesListf.add(0, varTimesF);
//
//                                    List<Integer> absTimesList = DeviceRecordFaultAndSynCache.recordWaveDFABSTimesMap
//                                            .get(UIElement.chooseDeviceAddr);
//                                    if (absTimesList == null) {
//                                        absTimesList = new Vector<Integer>();
//                                        DeviceRecordFaultAndSynCache.recordWaveDFABSTimesMap.put(
//                                                UIElement.chooseDeviceAddr, absTimesList);
//                                    }
//                                    if (absTimesList.size() >= 5) {
//                                        absTimesList.remove(4);
//                                    }
//                                    absTimesList.add(0, DeviceOptionFromCache.getSyncABSTimeToList().size() - 1);
                                    WaveDataCacheTemp.commCurrentMap.put(currentPack, new ArrayList<Integer>());
                                } else {
                                    List<Integer> waveDatalist = new Vector<Integer>();
                                    for (int jj = 0; jj < contentBuff.limit() / 2; jj++) {
                                        Integer dataTemp = contentBuff.getUnsignedShort(jj * 2);
                                        waveDatalist.add(dataTemp);
                                    }
                                    WaveDataCacheTemp.commCurrentMap.put(currentPack, waveDatalist);
                                }
                                CommandCache.commandMap.remove(CommandCache.SUPERADD_RECORD_POINT);
                                SuperAddRecordPointTread.lastRunTime = System.currentTimeMillis();
                                SuperAddRecordPointTread.isCheckData = true;
                                break;
                            }
                            case 0xcc: {
//                                if (length == 0x09) {
//                                    // 主机升级完成确认
//                                    byte sucByte = (byte) (totalBuf.get(i + 6) & 0x1);
//                                    if (sucByte == 1) {
//                                        UpdateProgramUICache.updateState = 10;// 完成
//                                    } else {
//                                        UpdateProgramUICache.updateState = 2;// 失败
//                                    }
//                                    break;
//                                }
//                                long dataAddrInt = UpdateProgramUICache.dataAddr;
//                                if (((byte) (dataAddrInt >> 24) == totalBuf.get(i + 5))
//                                        && ((byte) (dataAddrInt >> 16) == totalBuf.get(i + 6))
//                                        && ((byte) (dataAddrInt >> 8) == totalBuf.get(i + 7))
//                                        && ((byte) dataAddrInt == totalBuf.get(i + 8))) {
//
//                                    byte sucByte = (byte) (totalBuf.get(i + 9) & 0x1);
//                                    if (sucByte == 1) {
//                                        UpdateProgramUICache.dataAddr += UpdateProgramUICache.lastSendSize;
//                                        UpdateProgramUICache.updateState = 1;
//                                    } else {
//                                        UpdateProgramUICache.updateState = 2;
//                                    }
//                                }
//                                break;
                                if (length == 0x09) { // 升级包接收完成应答
                                    int sucByte = totalBuf.get(i + 6) & 0x01;
                                    if (sucByte == 1) {
                                        session.setAttribute("updateState", 10);
                                    } else {
                                        session.setAttribute("updateState", 2);
                                    }
                                } else { // 升级包接收应答
                                    int sucByte = totalBuf.get(i + 9) & 0x01;
                                    if (sucByte == 1) {
                                        session.setAttribute("dataAddr", totalBuf.getInt(i + 5));
                                        session.setAttribute("updateState", 1);
                                    } else {
                                        session.setAttribute("updateState", 2);
                                    }
                                }
                                break;

                            }

                            /**
                             * 指令升级指示数据回复：
                             */
                            case 0xfc: {
                                CmdDataModel cmdModel = CommandCache.commandMap.get(CommandCache.HOST_UPGRADE_CMD);
                                if (cmdModel != null) {
                                    if (cmdModel.getContentsList().size() > 0) {
                                        cmdModel.getContentsList().remove(0);
                                        cmdModel.setCounts((byte) 0);
                                    }
                                }
                                int result = totalBuf.get(i + 9) & 0xff;
                                if (result == 1) {
                                    new Thread(new ToastShowMsgThread(CommandCache.HOST_UPGRADE_CMD + "指令下发成功！")).start();
                                } else if (result == 0) {
                                    new Thread(new ToastShowMsgThread(CommandCache.HOST_UPGRADE_CMD + "指令下发失败！")).start();
                                } else if (result == 2) {
                                    new Thread(new ToastShowMsgThread("主机升级中,请稍后...")).start();
                                }
                                break;
                            }
                            /**
                             * 下面代码是通讯主机同步录波目录查询数据解析：
                             */
                            case 0xe0: {
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                int totalPack = totalBuf.get(i + 4) & 0xff;
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 6, length - 8);

                                int contentLengh = contentBuff.limit();

                                if (currentPack == 1) {
                                    WaveDataCacheTemp.syncLastFramIndex = 0;
                                    WaveDataCacheTemp.syncLastTimeIndex = -1;
                                } else {
                                    if ((WaveDataCacheTemp.syncLastFramIndex + 1) != currentPack) {
//                                        JOptionPane.showMessageDialog(null, "主机同步时间目录数据当前帧：" + currentPack + ";上帧号："
//                                                + WaveDataCacheTemp.syncLastFramIndex);
                                        WaveDataCacheTemp.syncLastFramIndex = 0;
                                        WaveDataCacheTemp.syncLastTimeIndex = -1;
                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }
                                }

                                WaveDataCacheTemp.syncLastFramIndex = currentPack;

                                for (int js = 0; js < contentLengh / 7; js++) {
                                    WaveDataCacheTemp.syncLastTimeIndex++;

                                    // 时间计算单位ms
                                    byte[] absTimeBytes = { contentBuff.get(0 + js * 7), contentBuff.get(1 + js * 7),
                                            contentBuff.get(2 + js * 7), contentBuff.get(3 + js * 7),
                                            contentBuff.get(4 + js * 7), contentBuff.get(5 + js * 7),
                                            contentBuff.get(6 + js * 7) };
                                    long absoluteTime = DateUtil101V2.getLongTimes(absTimeBytes);
//                                    RecordHostWaveData.catalogTimeForSync.put(WaveDataCacheTemp.syncLastTimeIndex,
//                                            absoluteTime);
                                    HostSyncRecordUI.contentsTimeList.add(WaveDataCacheTemp.syncLastTimeIndex,absoluteTime);
                                    HostSyncRecordFm.freshHostSyncDataModel(0);
                                }

                                if (totalPack == currentPack) {
                                    WaveDataCacheTemp.syncLastFramIndex = 0;
                                    WaveDataCacheTemp.syncLastTimeIndex = -1;
                                }
                                break;
                            }
                            /**
                             * 下面代码是通讯主机故障录波目录查询数据解析：
                             */
                            case 0xe1: {
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                int totalPack = totalBuf.get(i + 4) & 0xff;
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 6, length - 8);

                                int contentLengh = contentBuff.limit();

                                if (currentPack == 1) {
                                    WaveDataCacheTemp.faultLastFramIndex = 0;
                                    WaveDataCacheTemp.faultLastTimeIndex = -1;
                                } else {
                                    if ((WaveDataCacheTemp.faultLastFramIndex + 1) != currentPack) {
//                                        JOptionPane.showMessageDialog(null, "主机同步时间目录数据当前帧：" + currentPack + ";上帧号："
//                                                + WaveDataCacheTemp.faultLastFramIndex);
                                        WaveDataCacheTemp.faultLastFramIndex = 0;
                                        WaveDataCacheTemp.faultLastTimeIndex = -1;
                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }
                                }

                                WaveDataCacheTemp.faultLastFramIndex = currentPack;

                                for (int js = 0; js < contentLengh / 7; js++) {
                                    WaveDataCacheTemp.faultLastTimeIndex++;

                                    // 时间计算单位ms
                                    byte[] absTimeBytes = { contentBuff.get(0 + js * 7), contentBuff.get(1 + js * 7),
                                            contentBuff.get(2 + js * 7), contentBuff.get(3 + js * 7),
                                            contentBuff.get(4 + js * 7), contentBuff.get(5 + js * 7),
                                            contentBuff.get(6 + js * 7) };
                                    long absoluteTime = DateUtil101V2.getLongTimes(absTimeBytes);
//                                    RecordHostWaveData.catalogTimeForFault.put(WaveDataCacheTemp.faultLastTimeIndex,
//                                            absoluteTime);
                                    HostFaultRecordUI.contentsTimeList.add(WaveDataCacheTemp.faultLastTimeIndex,absoluteTime);
                                }

                                if (totalPack == currentPack) {
                                    WaveDataCacheTemp.faultLastFramIndex = 0;
                                    WaveDataCacheTemp.faultLastTimeIndex = -1;
                                }
                                break;
                            }
                            /**
                             * 下面代码是通讯主机录波查询数据解析：
                             */
                            case 0xec: {
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                int totalPack = totalBuf.get(i + 4) & 0xff;
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                if ((WaveDataCacheTemp.commHostECLastIndex == currentPack)
                                        || (WaveDataCacheTemp.commHostECLastIndex == -1 && currentPack == totalPack)) {
                                    break;
                                }
                                if (currentPack != 0) {
                                    if ((WaveDataCacheTemp.commHostECLastIndex + 1) != currentPack) {
//                                        JOptionPane.showMessageDialog(null, "主机同步电流数据当前帧：" + currentPack + ";上帧号："
//                                                + WaveDataCacheTemp.commHostECLastIndex);
                                        new Thread(new ToastShowMsgThread("主机同步电流数据当前帧：" + currentPack + ";上帧号："
                                                + WaveDataCacheTemp.commHostECLastIndex)).start();
                                        WaveDataCacheTemp.commHostECLastIndex = 0;

                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }
                                }
                                WaveDataCacheTemp.commHostECLastIndex = currentPack;

                                int indexData = totalBuf.get(i + 6) & 0xff;
                                int groupNum = totalBuf.get(i + 7) & 0xff;
                                int phase = totalBuf.get(i + 8) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 9, length - 11);

                                if (currentPack == 0) {
                                    // 时间计算单位ms
                                    byte[] absTimeBytes = { contentBuff.get(0), contentBuff.get(1), contentBuff.get(2),
                                            contentBuff.get(3), contentBuff.get(4), contentBuff.get(5), contentBuff.get(6) };
                                    long absoluteTime = DateUtil101V2.getLongTimes(absTimeBytes);
                                    /**
                                     * 更新时间目录
                                     */
//                                    RecordHostWaveData.catalogTimeForSync.put(indexData, absoluteTime);

                                    HostSyncRecordUI.contentsTimeList.add(indexData,absoluteTime);
                                    HostSyncRecordFm.freshHostSyncDataModel(0);

                                } else if (currentPack < totalPack) {
                                    if (currentPack == 1) {
                                        // 情况前面数据Host
                                        WaveDataCacheTemp.commHostECBuf = IoBuffer.allocate(0).setAutoExpand(true);
                                    }
                                    if (WaveDataCacheTemp.commHostECBuf == null) {

                                        new Thread(new ToastShowMsgThread("同步电流录波，缺少第一帧数据！")).start();
                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }
                                    WaveDataCacheTemp.commHostECBuf.put(contentBuff);
                                } else if (currentPack == totalPack) {
                                    IoBuffer allWaveData = WaveDataCacheTemp.commHostECBuf.put(contentBuff).duplicate();
                                    allWaveData.flip();
                                    List<Integer> waveDatalist = new Vector<Integer>();
                                    for (int jj = 0; jj < allWaveData.limit() / 2; jj++) {
                                        Integer dataTemp = allWaveData.getUnsignedShort(jj * 2);
                                        waveDatalist.add(dataTemp);
                                    }
                                    WaveDataCacheTemp.commHostECBuf = null;
                                    WaveDataCacheTemp.commHostECLastIndex = 0;
//                                    String type = "EC";
//                                    RecordHostWaveData.putHostWaveData(UIElement.chooseDeviceAddr, groupNum, phase, type,
//                                            indexData, waveDatalist);

                                    HostSyncRecordModel.putSyncElecMapModel(MainCahce.chooseDeviceAddr, groupNum, phase,
                                            indexData, waveDatalist);
                                    HostSyncRecordFm.freshHostSyncDataModel(1);
                                }
                                break;
                            }

                            case 0xed: {
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                int totalPack = totalBuf.get(i + 4) & 0xff;
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                if ((WaveDataCacheTemp.commHostEDLastIndex == currentPack)
                                        || (WaveDataCacheTemp.commHostEDLastIndex == -1 && currentPack == totalPack)) {
                                    break;
                                }
                                if (currentPack != 0) {
                                    if ((WaveDataCacheTemp.commHostEDLastIndex + 1) != currentPack) {
//                                        JOptionPane.showMessageDialog(null, "主机同步电压数据当前帧：" + currentPack + ";上帧号："
//                                                + WaveDataCacheTemp.commHostEDLastIndex);
                                        WaveDataCacheTemp.commHostEDLastIndex = 0;
                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }

                                }
                                WaveDataCacheTemp.commHostEDLastIndex = currentPack;

                                int indexData = totalBuf.get(i + 6) & 0xff;
                                int groupNum = totalBuf.get(i + 7) & 0xff;
                                int phase = totalBuf.get(i + 8) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 9, length - 11);

                                if (currentPack == 0) {
                                    // 时间计算单位ms
                                    byte[] absTimeBytes = { contentBuff.get(0), contentBuff.get(1), contentBuff.get(2),
                                            contentBuff.get(3), contentBuff.get(4), contentBuff.get(5), contentBuff.get(6) };
                                    long absoluteTime = DateUtil101V2.getLongTimes(absTimeBytes);
                                    /**
                                     * 更新时间目录
                                     */
//                                    RecordHostWaveData.catalogTimeForSync.put(indexData, absoluteTime);
                                    HostSyncRecordUI.contentsTimeList.add(indexData,absoluteTime);
                                    HostSyncRecordFm.freshHostSyncDataModel(0);
                                } else if (currentPack < totalPack) {
                                    if (currentPack == 1) {
                                        // 情况前面数据
                                        WaveDataCacheTemp.commHostEDBuf = IoBuffer.allocate(0).setAutoExpand(true);
                                    }
                                    if (WaveDataCacheTemp.commHostEDBuf == null) {
                                        new Thread(new ToastShowMsgThread("主机同步电压录波，缺少第一帧数据！")).start();
                                    }
                                    WaveDataCacheTemp.commHostEDBuf.put(contentBuff);
                                } else if (currentPack == totalPack) {
                                    IoBuffer allWaveData = WaveDataCacheTemp.commHostEDBuf.put(contentBuff).duplicate();
                                    allWaveData.flip();
                                    List<Integer> waveDatalist = new Vector<Integer>();
                                    for (int jj = 0; jj < allWaveData.limit() / 2; jj++) {
                                        Integer dataTemp = allWaveData.getUnsignedShort(jj * 2);
                                        waveDatalist.add(dataTemp);
                                    }
                                    WaveDataCacheTemp.commHostEDBuf = null;
                                    WaveDataCacheTemp.commHostEDLastIndex = 0;
//                                    String type = "ED";
//                                    RecordHostWaveData.putHostWaveData(UIElement.chooseDeviceAddr, groupNum, phase, type,
//                                            indexData, waveDatalist);
                                    HostSyncRecordModel.putSyncVoltMapModel(MainCahce.chooseDeviceAddr, groupNum, phase,
                                            indexData, waveDatalist);
                                    HostSyncRecordFm.freshHostSyncDataModel(1);
                                }
                                break;
                            }
                            case 0xee: {
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                int totalPack = totalBuf.get(i + 4) & 0xff;
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                if ((WaveDataCacheTemp.commHostEELastIndex == currentPack)
                                        || (WaveDataCacheTemp.commHostEELastIndex == -1 && currentPack == totalPack)) {
                                    break;
                                }
                                if (currentPack != 0) {
                                    if ((WaveDataCacheTemp.commHostEELastIndex + 1) != currentPack) {
//                                        JOptionPane.showMessageDialog(null, "主机故障电压数据当前帧：" + currentPack + ";上帧号："
//                                                + WaveDataCacheTemp.commHostEELastIndex);
                                        WaveDataCacheTemp.commHostEELastIndex = -1;
                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }

                                }
                                WaveDataCacheTemp.commHostEELastIndex = currentPack;

                                int indexData = totalBuf.get(i + 6) & 0xff;
                                int groupNum = totalBuf.get(i + 7) & 0xff;
                                int phase = totalBuf.get(i + 8) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 9, length - 11);

                                if (currentPack == 0) {
                                    byte[] absTimeBytes = { contentBuff.get(0), contentBuff.get(1), contentBuff.get(2),
                                            contentBuff.get(3), contentBuff.get(4), contentBuff.get(5), contentBuff.get(6) };
                                    long absoluteTime = DateUtil101V2.getLongTimes(absTimeBytes);
                                    /**
                                     * 更新时间目录
                                     */
//                                    RecordHostWaveData.catalogTimeForFault.put(indexData, absoluteTime);
                                    HostFaultRecordUI.contentsTimeList.add(indexData,absoluteTime);
                                    HostFaultRecordFm.freshHostSyncDataModel(0);

                                    // 时间计算单位ms
                                    long syncTimes0 = ((long) contentBuff.get(0 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(1 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(2 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(3 + 11) & 0xff);

                                    long syncTimes1 = ((long) contentBuff.get(4 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(5 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(6 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(7 + 11) & 0xff);
                                    // long syncTimes = syncTimes0*500*1000 +
                                    // (syncTimes1*2*1000*1000)/65536;
                                    long syncTimes = syncTimes0 * 16384 + syncTimes1;

                                    long faultTimes0 = ((long) contentBuff.get(8 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(9 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(10 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(11 + 11) & 0xff);

                                    long faultTimes1 = ((long) contentBuff.get(12 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(13 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(14 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(15 + 11) & 0xff);

                                    // long faultTimes = faultTimes0*500*1000 +
                                    // (faultTimes1*2*1000*1000)/65536;
                                    long faultTimes = faultTimes0 * 16384 + faultTimes1;

                                    long varTimes = faultTimes - syncTimes;

                                    WaveDataCacheTemp.varTimesHostEE = varTimes;

                                } else if (currentPack < totalPack) {
                                    if (currentPack == 1) {
                                        // 情况前面数据
                                        WaveDataCacheTemp.commHostEEBuf = IoBuffer.allocate(0).setAutoExpand(true);
                                    }
                                    if (WaveDataCacheTemp.commHostEEBuf == null) {
                                        new Thread(new ToastShowMsgThread("同步电压录波，缺少第一帧数据！")).start();
                                    }
                                    WaveDataCacheTemp.commHostEEBuf.put(contentBuff);
                                } else if (currentPack == totalPack) {
                                    if (WaveDataCacheTemp.varTimesHostEE == null) {
                                        new Thread(new ToastShowMsgThread("故障电流录波，缺少时间（0）帧数据！")).start();
                                    } else {

                                        IoBuffer allWaveData = WaveDataCacheTemp.commHostEEBuf.put(contentBuff).duplicate();
                                        allWaveData.flip();
                                        List<Integer> waveDatalist = new Vector<Integer>();
                                        for (int jj = 0; jj < allWaveData.limit() / 2; jj++) {
                                            Integer dataTemp = allWaveData.getUnsignedShort(jj * 2);
                                            waveDatalist.add(dataTemp);
                                        }

                                        String type = "EE";
//                                        RecordHostWaveData.putHostVarTimesMap(UIElement.chooseDeviceAddr, groupNum, phase,
//                                                type, indexData, WaveDataCacheTemp.varTimesHostEE);
//                                        RecordHostWaveData.putHostWaveData(UIElement.chooseDeviceAddr, groupNum, phase,
//                                                type, indexData, waveDatalist);
                                        HostFaultRecordModel.putFaultElecMapModel(MainCahce.chooseDeviceAddr, groupNum, phase,
                                                indexData, waveDatalist);
                                        HostFaultRecordFm.freshHostSyncDataModel(1);

                                        WaveDataCacheTemp.varTimesHostEE = null;
                                        WaveDataCacheTemp.commHostEEBuf = null;
                                        WaveDataCacheTemp.commHostEELastIndex = -1;

                                    }
                                }
                                break;
                            }
                            case 0xef: {
                                // 不具备可靠性数据整合,只根据是否是最后一帧数据
                                int totalPack = totalBuf.get(i + 4) & 0xff;
                                int currentPack = totalBuf.get(i + 5) & 0xff;
                                if ((WaveDataCacheTemp.commHostEFLastIndex == currentPack)
                                        || (WaveDataCacheTemp.commHostEFLastIndex == -1 && currentPack == totalPack)) {
                                    break;
                                }
                                if (currentPack != 0) {
                                    if ((WaveDataCacheTemp.commHostEFLastIndex + 1) != currentPack) {
//                                        JOptionPane.showMessageDialog(null, "主机故障电压数据当前帧：" + currentPack + ";上帧号："
//                                                + WaveDataCacheTemp.commHostEFLastIndex);
                                        WaveDataCacheTemp.commHostEFLastIndex = -1;
                                        session.setAttribute("DEVICE_LAST_BUF", null);
                                        return;
                                    }
                                }
                                WaveDataCacheTemp.commHostEFLastIndex = currentPack;

                                int indexData = totalBuf.get(i + 6) & 0xff;
                                int groupNum = totalBuf.get(i + 7) & 0xff;
                                int phase = totalBuf.get(i + 8) & 0xff;
                                IoBuffer contentBuff = totalBuf.duplicate().getSlice(i + 9, length - 11);

                                if (currentPack == 0) {
                                    byte[] absTimeBytes = { contentBuff.get(0), contentBuff.get(1), contentBuff.get(2),
                                            contentBuff.get(3), contentBuff.get(4), contentBuff.get(5), contentBuff.get(6) };
                                    long absoluteTime = DateUtil101V2.getLongTimes(absTimeBytes);
                                    /**
                                     * 更新时间目录
                                     */
//                                    RecordHostWaveData.catalogTimeForFault.put(indexData, absoluteTime);
                                    HostFaultRecordUI.contentsTimeList.add(indexData,absoluteTime);
                                    HostFaultRecordFm.freshHostSyncDataModel(0);

                                    // 时间计算单位ms
                                    long syncTimes0 = ((long) contentBuff.get(0 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(1 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(2 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(3 + 11) & 0xff);

                                    long syncTimes1 = ((long) contentBuff.get(4 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(5 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(6 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(7 + 11) & 0xff);
                                    // long syncTimes = syncTimes0*500*1000 +
                                    // (syncTimes1*2*1000*1000)/65536;
                                    long syncTimes = syncTimes0 * 16384 + syncTimes1;

                                    long faultTimes0 = ((long) contentBuff.get(8 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(9 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(10 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(11 + 11) & 0xff);

                                    long faultTimes1 = ((long) contentBuff.get(12 + 11) & 0xff) * 256 * 256 * 256
                                            + ((long) contentBuff.get(13 + 11) & 0xff) * 256 * 256
                                            + ((long) contentBuff.get(14 + 11) & 0xff) * 256
                                            + ((long) contentBuff.get(15 + 11) & 0xff);

                                    // long faultTimes = faultTimes0*500*1000 +
                                    // (faultTimes1*2*1000*1000)/65536;
                                    long faultTimes = faultTimes0 * 16384 + faultTimes1;

                                    long varTimes = faultTimes - syncTimes;

                                    WaveDataCacheTemp.varTimesHostEF = varTimes;

                                } else if (currentPack < totalPack) {
                                    if (currentPack == 1) {
                                        // 情况前面数据
                                        WaveDataCacheTemp.commHostEFBuf = IoBuffer.allocate(0).setAutoExpand(true);
                                    }
                                    if (WaveDataCacheTemp.commHostEFBuf == null) {
                                        new Thread(new ToastShowMsgThread("故障电压录波，缺少第一帧数据！")).start();
                                    }
                                    WaveDataCacheTemp.commHostEFBuf.put(contentBuff);
                                } else if (currentPack == totalPack) {
                                    if (WaveDataCacheTemp.varTimesHostEF == null) {
                                        new Thread(new ToastShowMsgThread("故障电压录波，缺少时间（0）帧数据！")).start();
                                    } else {

                                        IoBuffer allWaveData = WaveDataCacheTemp.commHostEFBuf.put(contentBuff).duplicate();
                                        allWaveData.flip();
                                        List<Integer> waveDatalist = new Vector<Integer>();
                                        for (int jj = 0; jj < allWaveData.limit() / 2; jj++) {
                                            Integer dataTemp = allWaveData.getUnsignedShort(jj * 2);
                                            waveDatalist.add(dataTemp);
                                        }
                                        String type = "EF";
//                                        RecordHostWaveData.putHostWaveData(UIElement.chooseDeviceAddr, groupNum, phase,
//                                                type, indexData, waveDatalist);
//                                        RecordHostWaveData.putHostVarTimesMap(UIElement.chooseDeviceAddr, groupNum, phase,
//                                                type, indexData, WaveDataCacheTemp.varTimesHostEF);
                                        HostFaultRecordModel.putFaultVoltMapModel(MainCahce.chooseDeviceAddr, groupNum, phase,
                                                indexData, waveDatalist);
                                        HostFaultRecordFm.freshHostSyncDataModel(1);

                                        WaveDataCacheTemp.varTimesHostEF = null;
                                        WaveDataCacheTemp.commHostEFBuf = null;
                                        WaveDataCacheTemp.commHostEFLastIndex = -1;
                                    }
                                }
                                break;
                            }

                            default:
                                break;
                        }

                        // 本地参数查询应答
                        i += length - 1;
                    } else {
                        System.out.println("crc校验错误!");
                    }
                } else {
                    IoBuffer leaveBuf = IoBuffer.allocate(0).setAutoExpand(true);
                    leaveBuf.put(totalBuf.getSlice(i, totalBuf.limit() - i)).flip();
                    session.setAttribute("DEVICE_LAST_BUF", leaveBuf);
                    System.out.println("报文长度不对,格式长度：" + length + "; 实际报文：" + totalBuf.limit());
                    return;
                }
            }
        }
    }

}
